using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Common = Microsoft.ConnectionEngine.Registers.Health.Patient.Common;

namespace Microsoft.ConnectionEngine.Registers.Health.Patient.DataAccess
{
    public class Person
    {
        #region Update/Insert

        /// <summary>
        /// Inserts a New Person Record
        /// </summary>
        /// <returns>Returns a PersonID</returns>
        public static int PersonInsert(DateTime dDateOfBirth,
	                                    DateTime dDateOfDeath,
	                                    string sGender,
	                                    string sUpdateUserID)
        {
            string SQL = DataAccessResources.PersonInsertProc;
            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());

            SqlCommand dbCommand = new SqlCommand();
            dbCommand.CommandText = SQL;
            dbCommand.CommandType = CommandType.StoredProcedure;

            dbCommand.Parameters.AddWithValue("@DateOfBirth", DataAccessHelper.IfNull(dDateOfBirth));
            dbCommand.Parameters.AddWithValue("@DateOfDeath", DataAccessHelper.IfNull(dDateOfDeath));
            dbCommand.Parameters.AddWithValue("@Gender", DataAccessHelper.IfNull(sGender));
            dbCommand.Parameters.AddWithValue("@UpdateUserID", DataAccessHelper.IfNull(sUpdateUserID));

           
            int sNewPersonID = Convert.ToInt32(db.ExecuteScalar(dbCommand));

            return sNewPersonID;

        }

        /// <summary>
        /// updates a current Person
        /// </summary>
        /// <returns>Returns a boolean, set to True if person was found and updated</returns>
        public static bool PersonUpdate(string sPatientID,
	                                    DateTime dDateOfBirth,
                                        DateTime dDateOfDeath,
	                                    string sGender,
	                                    string sUpdateUserID)
        {
            string SQL = DataAccessResources.PersonUpdateProc;
            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());

            SqlCommand dbCommand = new SqlCommand();
            dbCommand.CommandText = SQL;
            dbCommand.CommandType = CommandType.StoredProcedure;

	        dbCommand.Parameters.AddWithValue("@PatientID", DataAccessHelper.IfNull(sPatientID));
	        dbCommand.Parameters.AddWithValue("@DateOfBirth", DataAccessHelper.IfNull(dDateOfBirth));
	        dbCommand.Parameters.AddWithValue("@DateOfDeath", DataAccessHelper.IfNull(dDateOfDeath));
	        dbCommand.Parameters.AddWithValue("@Gender", DataAccessHelper.IfNull(sGender));
	        dbCommand.Parameters.AddWithValue("@UpdateUserID", DataAccessHelper.IfNull(sUpdateUserID));

            int iRowCount = db.ExecuteNonQuery(dbCommand);

            return (iRowCount == 1);

        }


        #endregion

    }
}
